﻿
package com.victim.snake.graphics.roomClass
{
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.display.LineScaleMode;
    import flash.display.CapsStyle;
    import flash.display.JointStyle;
    import fl.controls.Label;
	import fl.managers.StyleManager;
	import com.victim.snake.style.TextStyle;
    import com.victim.snake.util.GraphicsToll;
    import com.victim.snake.player.Player;
    import com.victim.snake.user.User;
    import flash.filters.GlowFilter;
    import com.victim.snake.player.PlayerEvent;
    import com.victim.snake.snake.SnakeStyle;
    import com.victim.snake.snake.SnakeStyleEvent;
    import flash.display.Bitmap;
	


	//房间界面用户框
	public class ShowPlayerSprite extends Sprite
	{
		static var playerStatusMasterGlowFilter:GlowFilter=new GlowFilter(0xffffbe,0.75,4,4,8,1,false,false);
		static var playerStatusReadyGlowFilter:GlowFilter=new GlowFilter(0xffffbe,0.75,4,4,8,1,false,false);
		static var groupFilterArray:Array=new Array(
													new GlowFilter(0xff0000,1,9,9,4,1,false,false)
													,new GlowFilter(0x57e700,1,9,9,6,1,false,false)
													,new GlowFilter(0xfc8b01,1,9,9,4,1,false,false)
													,new GlowFilter(0x8f3ff0,1,9,9,4,1,false,false));	//队伍颜色外发光滤镜
		
		private var nameLab:Label=null;
		public var player:Player=null;
		private var readyStatusLab:Label=null;	//显示用户状态lab  (房主、准备、取消准备)
		private var uidLab:Label=null;	//用户ID，测试用
		private var snakeStyle:SnakeStyle=null;	//玩家蛇样式
		private var bigThumbnailsImage:Bitmap=null;	//玩家蛇样式大缩略图
		
		
		public function ShowPlayerSprite(player:Player=null):void
		{

			/*注释2013.05.29   将简陋的边框注释掉
			var graphics:Graphics = this.graphics;
			graphics.lineStyle(0, 0xcccccc, 1, false, LineScaleMode.VERTICAL,CapsStyle.NONE, JointStyle.MITER, 10);
			//头像框
			GraphicsToll.drawRectBorder(graphics,0,0,100,85);
			//名字框
			GraphicsToll.drawRectBorder(graphics,0,87,100,33);
			*/					
			
			
			//测试用,用户id
			this.uidLab=new Label();
			this.uidLab.x=0;
			this.uidLab.y=0;
			this.uidLab.width=26
			this.uidLab.text="";
			this.addChild(uidLab);
			
			//显示用户名
			nameLab=new Label();
			nameLab.width=94;
			nameLab.x=3;
			nameLab.y=85;
			nameLab.text="";
			nameLab.setStyle("textFormat",TextStyle.getShowPlayerSpritePlayerNameStyle());
			this.addChildAt(nameLab,0);
			
			//显示用户状态	(房主、准备、取消准备)
			readyStatusLab=new Label();
			readyStatusLab.width=94;
			readyStatusLab.x=3;
			readyStatusLab.y=101;
			readyStatusLab.text="";
			this.addChildAt(readyStatusLab,1);
			
			
			if(player!=null){
				this.setPlayer(player);
			}
			
			
		}
		
		//显示玩家蛇样式大缩略图方法
		private function showBigThumbnailsLoaded(e:SnakeStyleEvent):void{
			snakeStyle.removeEventListener(SnakeStyleEvent.IsLoaded,showBigThumbnailsLoaded);
			if(bigThumbnailsImage!=null){
				this.removeChild(bigThumbnailsImage);
			}
			bigThumbnailsImage=new Bitmap(e.snakeStyle.bigThumbnailsImage.bitmapData);
			
			//添加发光滤镜			
			var filtersArray:Array=bigThumbnailsImage.filters;
			filtersArray[0]=ShowPlayerSprite.groupFilterArray[this.player.getGroup().groupId];
			bigThumbnailsImage.filters=filtersArray;
			
			this.addChild(bigThumbnailsImage);
		}
		
		//初使化
		public function init():void{
			//如果之前有与此显示用户框关联的用户对象,则删除在此用户对象上的监听
			if(this.player!=null){
				//删除玩家状态改变监听
				this.player.removeEventListener(PlayerEvent.Ready,playerReadyEvent);
				this.player.removeEventListener(PlayerEvent.UnReady,playerUnReadyEvent);
				this.player.removeEventListener(PlayerEvent.SnakeStyleChange,snakeStyleChange);
				this.player.removeEventListener(PlayerEvent.SnakeGroupChange,snakeGroupChange);
			}
			this.player=null;	//将与此显示用户框关联的用户对象设为null
			this.nameLab.text="";
			this.readyStatusLab.text="";
			if(this.bigThumbnailsImage!=null){
				this.removeChild(this.bigThumbnailsImage);	//删除蛇样式大缩略图
				this.bigThumbnailsImage=null;
			}
		}
		
		//设置showPlayerSprite所对应的玩家
		public function setPlayer(player:Player){
			this.player=player;
			this.setPlayerName(this.player.getUserName());
			if(User.getRoom().getMaster()==this.player){
				this.readyStatusLab.text="房        主";
				//添加发光滤镜
				var filtersArray:Array=this.readyStatusLab.filters;
				filtersArray[0]=ShowPlayerSprite.playerStatusReadyGlowFilter;
				this.readyStatusLab.filters=filtersArray;
				this.readyStatusLab.setStyle("textFormat",TextStyle.getShowPlayerSpriteMasterStatusStyle());
			}else{
				//显示准备或等待
				if(player.getIsReady()){
					this.readyStatusLab.setStyle("textFormat",TextStyle.getShowPlayerSpriteReadyStatusStyle());	//设置样式为末准备样式
					//添加准备滤镜
					filtersArray=this.readyStatusLab.filters;
					filtersArray.splice(0);
					filtersArray[0]=ShowPlayerSprite.playerStatusReadyGlowFilter;
					this.readyStatusLab.filters=filtersArray;
					//显示“准备”文字信息
					this.readyStatusLab.text="准        备";
				}else{
					//设置样式
					this.readyStatusLab.setStyle("textFormat",TextStyle.getShowPlayerSpriteUnReadyStatusStyle());
					//去除所有滤镜
					filtersArray=this.readyStatusLab.filters;
					filtersArray.splice(0);
					this.readyStatusLab.filters=filtersArray;
					//显示“等待”文字信息
					this.readyStatusLab.text="等        待";
				}
				
				
			}
			//this.uidLab.text=player.getUid()+"";
			
			//取得玩家选择的蛇的样式
			snakeStyle=SnakeStyle.getStyle(player.getSnakeStyle());			
			//添加监听方法，如果玩家蛇样式大缩略图下载完成,调用显示玩家蛇样式大缩略图方法
			snakeStyle.addEventListener(SnakeStyleEvent.IsLoaded,showBigThumbnailsLoaded);
			//加载样式资源
			snakeStyle.loading();
			
			//添加玩家状态改变监听
			this.player.addEventListener(PlayerEvent.Ready,playerReadyEvent);
			this.player.addEventListener(PlayerEvent.UnReady,playerUnReadyEvent);
			this.player.addEventListener(PlayerEvent.SnakeStyleChange,snakeStyleChange);
			this.player.addEventListener(PlayerEvent.SnakeGroupChange,snakeGroupChange);
		}
		
		//玩家选择新的蛇样式时蛇样式改变事件
		public function snakeStyleChange(e:PlayerEvent):void{
			//取得玩家选择的蛇的样式
			snakeStyle=SnakeStyle.getStyle(player.getSnakeStyle());			
			//添加监听方法，如果玩家蛇样式大缩略图下载完成,调用显示玩家蛇样式大缩略图方法
			snakeStyle.addEventListener(SnakeStyleEvent.IsLoaded,showBigThumbnailsLoaded);
			//加载样式资源
			snakeStyle.loading();
		}
		
		
		//玩家状态改变为ready（准备）
		public function playerReadyEvent(e:PlayerEvent){
			//如果改变状态用户不为房主
			if(e.player!=User.getRoom().getMaster()){
				this.readyStatusLab.setStyle("textFormat",TextStyle.getShowPlayerSpriteReadyStatusStyle());	//设置样式为末准备样式
				//添加所有滤镜
				var filtersArray:Array=this.readyStatusLab.filters;
				filtersArray[0]=ShowPlayerSprite.playerStatusReadyGlowFilter;
				this.readyStatusLab.filters=filtersArray;
				//显示“准备”文字信息
				this.readyStatusLab.text="准        备";
			}
		}
		
		//玩家状态改变为unReady（取消准备）
		public function playerUnReadyEvent(e:PlayerEvent){
			//如果改变状态用户不为房主
			if(e.player!=User.getRoom().getMaster()){
				this.readyStatusLab.setStyle("textFormat",TextStyle.getShowPlayerSpriteUnReadyStatusStyle());	//设置样式为末准备样式
				//去除发光滤镜
				var filtersArray:Array=this.readyStatusLab.filters;
				filtersArray.splice(0);
				this.readyStatusLab.filters=filtersArray;
				//显示“等待”文字信息
				this.readyStatusLab.text="等        待";
			}
		}
		
		public function setPlayerName(playerName:String):void{
			//this.nameLab.setStyle("textFormat",TextStyle.getDefaultLableStyle());	//设置样式为默认样式
			this.nameLab.text=playerName;
		}
		
		
		//玩家队伍改变事件
		public function snakeGroupChange(e:PlayerEvent):void{
			//更改队伍发光滤镜			
			var filtersArray:Array=bigThumbnailsImage.filters;
			filtersArray.splice(0);	//删除所有滤镜
			filtersArray[0]=ShowPlayerSprite.groupFilterArray[this.player.getGroup().groupId];	//添加新滤镜
			bigThumbnailsImage.filters=filtersArray;
		}

	}

}